/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import com.openbrew.openbrewrf.Administration;
import com.openbrew.openbrewrf.InvSalesLines;
import com.openbrew.openbrewrf.Sales;
import com.openbrew.openbrewrf.SalesOrder;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 *
 * @author chapmand
 */
public class CreateSimpleSalesOrderView {
    private String overViewString = "";
    private float salesOrderTotalCost = 0;
        
    final NumberFormat formatter = new DecimalFormat("$###.##");
    final NumberFormat money = new DecimalFormat("$#0.00");
    
    public SimpleSalesOrderView getSalesOrderString(final SalesOrder so, final Administration admin) {
        //reset globals
        overViewString = "";
        salesOrderTotalCost = 0;
        float totalCost = 0;
        int numKegs = 0;

        if(so.getSalesCollection()!=null){
            for (final Sales sale : so.getSalesCollection()) {
                if(sale.getSalesFGIID() == null){
                    //This is an old style entry, ignore!
                } else {
                    salesOrderTotalCost += (float)sale.getLineSubTot();
                    overViewString += sale.getSalesString(admin);
                    //Determine if it is a keg, if so increment.
                    if(sale.getSalesFGIID().getFGIPackageID().getPackageTypeVol() >= 0.03){
                        numKegs++;
                    }
                }
                totalCost += salesOrderTotalCost;
                salesOrderTotalCost = 0;
            }
        }

        if(so.getInvSalesLinesCollection()!=null){
            salesOrderTotalCost = 0;
            for (final InvSalesLines isale : so.getInvSalesLinesCollection()) {
                if(isale.getInvSaleSupplyID()!=null){
                    salesOrderTotalCost += (float)isale.getLineSubTot();
                    overViewString += isale.getSalesString();
                }
                totalCost += salesOrderTotalCost;
                salesOrderTotalCost = 0;
            }
        }
        
        if(so.getSOCust().getCdist() && admin.getDistKegDeposit()>0){
            float deposit = numKegs * admin.getDistKegDeposit();
            if(deposit > 0){
                totalCost += deposit;
                overViewString += getDepositLineItem(admin.getDistKegDeposit(), deposit, numKegs);
            }
        } else if(!so.getSOCust().getCdist() && admin.getNonDistKegDeposit()>0){
            float deposit = numKegs * admin.getNonDistKegDeposit();
            if(deposit > 0){
                totalCost += deposit;
                overViewString += getDepositLineItem(admin.getNonDistKegDeposit(), deposit, numKegs);
            }
        }
        
        return new SimpleSalesOrderView(totalCost, overViewString, numKegs);
    }
    
    private String getDepositLineItem(final float depositPrice, final float totalDeposit, final int numKegs){
        String str = "Keg Deposit";
        str +="," + money.format(depositPrice);
        str +="," + Integer.toString(numKegs);
        str +="," + money.format(totalDeposit) + "\n";
        return str;
        
    }
}
